﻿using ZhuXingFamilys.Database;
using System;
using System.Collections;
using System.Windows;
using System.Windows.Input;
using ZhuXingFamilys.Help;
using System.IO;
using System.Windows.Controls;
using MaterialDesignThemes.Wpf;
using MaterialDesignColors;
using Autodesk.Revit.UI;
using ZhuXingFamilys.MainViewModel;
using ZhuXingFamilys.utils;

namespace ZhuXingFamilys.User
{
    /// <summary>
    /// UserLogin.xaml 的交互逻辑
    /// </summary>
    public partial class UserLogin : Window
    {
        public UserLogin(string username, string passs)
        {
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            WindowStartupLocation = WindowStartupLocation.CenterScreen; InitializeComponent();
            UserName = username;
            Account.Text = username;
            Password.Password = passs;
            buttonAvailabilityClass = new ButtonAvailabilityClass();
            ButtonAvailabilityClassEvent = ExternalEvent.Create(buttonAvailabilityClass);
        }
        ButtonAvailabilityClass buttonAvailabilityClass = null;
        ExternalEvent ButtonAvailabilityClassEvent = null;


        public string UserName;
        public string UserPassword;
        public string LoginTime;
        public string IsRemember;
        public int border = 1;
        public static Hashtable userall_harsh;

        private void MoveWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public const string CONFIG_LOGIN_CONTENT = "LoginInfo";
        public const string CONFIG_LOGIN_USERNAME_CONTENT = "UserName";
        public const string CONFIG_LOGIN_PASS_CONTENT = "UserPassword";
        public const string CONFIG_LOGIN_LOGINTIME_CONTENT = "LoginTime";
        public const string CONFIG_LOGIN_IsRemember_CONTENT = "IsRemember";
        public const string CONFIG_LOGIN_Authority_CONTENT = "Authority";

        #region SQL server 使用账户数据
        private void Login_Button(object sender, RoutedEventArgs e)
        {
            string userName = this.Account.Text;
            string password = this.Password.Password;
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                SqlserverDatabase sqlserver = new SqlserverDatabase();
                T_User LoginUser = sqlserver.Login(userName, password);
                if (LoginUser == null)
                {
                    MessageBox.Show("用户名或密码错误！");
                }
                else
                {
                    if (LoginUser.Authority == -1)
                    {
                        MessageBox.Show("当前用户没有任何插件使用权限,请联系管理员添加！");
                    }
                    else
                    {
                        ButtonAvailabilityClassEvent.Raise();
                        MessageBox.Show("登录成功");
                        string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        string iniFilePath = "LoginInfo.ini";
                        string CurAppDir = Set.getaddress();

                        //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
                        if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
                        {
                            iniFilePath = Path.Combine(CurAppDir, iniFilePath);
                        }
                        Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_LOGINTIME_CONTENT, NowTime, iniFilePath);
                        if (Convert.ToBoolean(ckbRemember.IsChecked))
                        {
                            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_IsRemember_CONTENT, "true", iniFilePath);
                            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_PASS_CONTENT, Password.Password.ToString(), iniFilePath);
                        }
                        else
                        {
                            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_IsRemember_CONTENT, "false", iniFilePath);
                            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_PASS_CONTENT, "", iniFilePath);
                        }
                        Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_USERNAME_CONTENT, LoginUser.UserName, iniFilePath);

                        Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_Authority_CONTENT, LoginUser.Authority.ToString(), iniFilePath);

                        this.DialogResult = Convert.ToBoolean(1);
                        this.Close();
                        // 根据权限启用按钮
                        PanelEnableDisableUtils.CheckAuthority(LoginUser.Authority);
                        UserCenter userCenter = new UserCenter();
                        System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(userCenter);
                        mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                        userCenter.Show();
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入用户名和密码");
            }

            //SqlserverDatabase sqlserver = new SqlserverDatabase();
            //userall_harsh = sqlserver.sqlserver_read();


            //if (userall_harsh == null)
            //{
            //    MessageBox.Show("无此账户，请先注册!");
            //    return;
            //}
            //else
            //{
            //    IDictionaryEnumerator myEnumerator = userall_harsh.GetEnumerator();  //读取harshtable中的key和value值
            //    while (myEnumerator.MoveNext()) //将枚举数推到集合的下一元素，若为空，则退出循环
            //    {

            //        UserName = myEnumerator.Key.ToString();         //key值赋给UserName
            //        UserPassword = myEnumerator.Value.ToString();    //value值赋给UserPassword

            //        if (Account.Text.ToString() == UserName && Password.Password.ToString() == UserPassword)
            //        {
            //            ButtonAvailabilityClassEvent.Raise();
            //            MessageBox.Show("登录成功");
            //            string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //            string iniFilePath = "LoginInfo.ini";
            //            string CurAppDir = Set.getaddress();

            //            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            //            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            //            {
            //                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            //            }
            //            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_LOGINTIME_CONTENT, NowTime,  iniFilePath);
            //            if (Convert.ToBoolean(ckbRemember.IsChecked))
            //            {
            //                Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_IsRemember_CONTENT, "true", iniFilePath);
            //                Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_PASS_CONTENT, Password.Password.ToString(), iniFilePath);
            //            }
            //            else {

            //                Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_IsRemember_CONTENT, "false", iniFilePath);
            //                Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_PASS_CONTENT, "", iniFilePath);
            //            }
            //            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_USERNAME_CONTENT, UserName, iniFilePath);

            //            this.DialogResult = Convert.ToBoolean(1);
            //            this.Close();
            //            UserCenter userCenter = new UserCenter();
            //            System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(userCenter);
            //            mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            //            userCenter.Show();
            //            break;
            //        }
            //        else if (border <= userall_harsh.Count - 1)      //给循环一边界，若循环到所存数据最后一个数仍然不正确，则执行else语句
            //        {
            //            border++;
            //        }
            //        else
            //            MessageBox.Show("账号或密码错误，请重试！");
            //    }
            //}

        }


        #endregion

        //“注册账户”TextBlock触发事件
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RegisterWindow register1 = new RegisterWindow();  //Login为窗口名，把要跳转的新窗口实例化
            this.Close();  //关闭当前窗口
            register1.ShowDialog();   //打开新窗口          
        }

        private void ReClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        // 忘记密码
        private void Forget_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            forget forget = new forget();  //Login为窗口名，把要跳转的新窗口实例化
            this.Close();  //关闭当前窗口
            forget.ShowDialog();   //打开新窗口       
        }

        // 免费试用
        private void Free_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FreeTrial freetrial = new FreeTrial();  //Login为窗口名，把要跳转的新窗口实例化
            this.Close();  //关闭当前窗口
            freetrial.ShowDialog();   //打开新窗口       
        }
    }
}
